import transaction
from ZODB import FileStorage, DB
from persistent.mapping import PersistentMapping

# 打开数据库连接
storage = FileStorage.FileStorage('mydatabase.fs')
db = DB(storage)
connection = db.open()
root = connection.root()

# 确保testcases容器存在
if not hasattr(root, 'testcases'):
    root.testcases = PersistentMapping()

# 读取并打印所有对象
print("当前数据库中的对象：")
for key, value in root.testcases.items():
    print(f"ID: {key}, Value: {value}")

# 写入一个新对象
new_id = len(root.testcases) + 1
root.testcases[new_id] = f"Test Object {new_id}"
transaction.commit()

# 关闭数据库连接
connection.close()
db.close()
storage.close()